#include "GlutCallbacks.h"
#include "CVGlutViewer.h"

#define CreateMenuHandlerWrapper(EntryName) \
	void EntryName##MenuHandlerWrapper(int value)	\
	{\
		pApp->EntryName##MenuHandler(value);	\
	}

static CCVGlutViewer* pApp = NULL;

void RenderSceneCB()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	pApp->Render();
	glutSwapBuffers();
}

static void NormalKeyboardCB(uchar key, int x, int y)
{
	pApp->OnKey(key, x, y);
}

static void PassiveMouseCB(int x, int y)
{
	if (pApp->IsCamMoveAble())
	{
		pApp->GetViewCamera().OnMouse(x, y);
	}
}

static void KeyboardUPCB(uchar key, int x, int y)
{
	pApp->OnUpKey(key, x, y);
}

static void MouseCB(int button, int state, int x, int y)
{
	pApp->MouseClick(button, state, x, y);
}

static void MotionCB(int x, int y)
{
	pApp->MouseMotion(x, y);
}

static void MouseWheelCB(int button, int dir, int x, int y)
{
	pApp->MouseWheel(dir);
}
void InitializeGlutCallbacks(CCVGlutViewer* _pApp)
{

	pApp = _pApp;

	glutDisplayFunc(RenderSceneCB);
	glutIdleFunc(RenderSceneCB);
	glutKeyboardFunc(NormalKeyboardCB);
	glutKeyboardUpFunc(KeyboardUPCB);
	glutPassiveMotionFunc(PassiveMouseCB);
	glutMouseFunc(MouseCB);
	glutMotionFunc(MotionCB);
	glutMouseWheelFunc(MouseWheelCB);
}

CreateMenuHandlerWrapper(File)

CreateMenuHandlerWrapper(Viewer);

